// pages/view/fs/patientArrears/patientArrears.js
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    chartType: 1,

    changeFlag_dept: true,
    changeFlag_patient: false,

    //背景颜色
    deptBg: '#37a2da',
    patientBg: '#C1C1C1',

    chartTitle_1: '科室出院病人欠款坏账统计',
    cloumn_data: [15, 20, 45, 37, 55, 77],
    cloumn_data_2: [20, 18, 55, 35, 45, 37],
    cloumn_data_3: [10, 8, 5, 3, 4, 7],
    cloumn_categories: ['科室1', '科室2', '科室3', '科室4', '科室5', '科室6'],

    //科室统计
    dept_data_list:[],
    dept_sum_data:{},

    //病人清单

    patient_data_list:[],
    patient_sum_data:{}


  },

  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.myRequest(options)

  },
  /**
 * 调用封装好的网络请求，获取数据
 */
  myRequest: function (options) {
    var that = this;

    console.log('options:', options)
    //调用封装好的网络请求，获取数据
    network.get('/fs/arrears?startTime=' + options.startTime + '&endTime=' + options.endTime, {}).then(function (res) {

      console.log('res:', res)
      var obj_table = res.data.data.list1
      var obj_dept = res.data.data.list2
      var obj_total_data = res.data.data.list3
      var obj_deposit_data = res.data.data.list4
      var obj_badDebt_data = res.data.data.list5
      var obj_sum_data = res.data.data.list6

      var obj_patient_data = res.data.data_2.list1
      var obj_sum_patient_data = res.data.data_2.list2
      // console.log("obj_table:", obj_table)
      // console.log("obj_dept:", obj_dept)
      // console.log("obj_total_data:", obj_total_data)
      // console.log("obj_deposit_data:", obj_deposit_data)
      // console.log("obj_badDebt_data:", obj_badDebt_data)
      // console.log("obj_sum_data:", obj_sum_data)
      that.setData({
        'cloumn_categories': obj_dept,
        'cloumn_data': obj_total_data,
        'cloumn_data_2': obj_deposit_data,
        'cloumn_data_3': obj_badDebt_data,
        'dept_data_list': obj_table,
        'sum_data': obj_sum_data,
        'patient_data_list': obj_patient_data,
        'patient_sum_data': obj_sum_patient_data
      })
    }).then(function (res) {
      wx.hideLoading()
      that.createColumnChart();

    }).catch(function (res) {
      console.log(res)
    })
  },
  /**
 * 创建 柱状图
 */
  createColumnChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.cloumn_categories,
      series: [{
        name: '合计',
        data: that.data.cloumn_data,
        format: function (val, name) {

          return val.toFixed(2) + '';
        }
      }, {
          name: '预交款',
          data: that.data.cloumn_data_2,
          format: function (val, name) {

            return val.toFixed(2) + '';
          }
        }, {
          name: '坏账',
          data: that.data.cloumn_data_3,
          format: function (val, name) {

            return val.toFixed(2) + '';
          }
        }
      
      ],
      yAxis: {
        format: function (val) {
          return val + '（元）';
        },
        title: '金额',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 250,
    });
  },

  changeToDept: function (e) {
    var that = this;
    var flag = that.data.changeFlag_dept;
    console.log('flag:', flag)
    if (!flag) {
      that.data.changeFlag_dept = true
      that.setData({
        'changeFlag_dept': true,
        'changeFlag_patient': false,
        'deptBg': "#37a2da",
        'patientBg': "#C1C1C1",
        chartType: 1
      })

      that.createColumnChart();



    }


  },
  changeToPaitent: function (e) {
    var that = this;
    var flag = that.data.changeFlag_patient;
    if (!flag) {
      that.setData({
        'changeFlag_dept': false,
        'changeFlag_patient': true,
        'deptBg': "#C1C1C1",
        'patientBg': "#32c5e9",
        chartType: 2
      })
     // that.createPieChart();

    }
  },

})